package com.yuanrui.practice.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.shaded.com.google.common.base.Preconditions;
import com.yuanrui.practice.api.common.PageResult;
import com.yuanrui.practice.api.common.Result;
import com.yuanrui.practice.api.req.*;
import com.yuanrui.practice.api.vo.PracticeSetVO;
import com.yuanrui.practice.api.vo.PracticeSubjectListVO;
import com.yuanrui.practice.api.vo.PracticeSubjectVO;
import com.yuanrui.practice.api.vo.SpecialPracticeVO;
import com.yuanrui.practice.server.entity.dto.PracticeSetDTO;
import com.yuanrui.practice.server.entity.dto.PracticeSubjectDTO;
import com.yuanrui.practice.server.service.PracticeDetailService;
import com.yuanrui.practice.server.service.PracticeSetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 练习套卷
 */
@RestController
@Slf4j
@RequestMapping("/practice/set")
public class PracticeSetController {

    @Resource
    private PracticeSetService practiceSetService;

    @Resource
    private PracticeDetailService practiceDetailService;

    @RequestMapping("getSpecialPracticeContent")
    public Result<List<SpecialPracticeVO>> getSpecialPracticeContent() {
        try {
            List<SpecialPracticeVO> practiceContent = practiceSetService.getSpecialPracticeContent();
            if (log.isInfoEnabled()) {
                log.info("getSpecialPracticeContent:{}", practiceContent);
            }
            return Result.ok(practiceContent);
        } catch (Exception e) {
            log.error("getSpecialPracticeContent.error{}", e.getMessage(), e);
            return Result.fail("获取专项练习内容失败");
        }
    }

    @RequestMapping("/addPractice")
    public Result<PracticeSetVO> addPractice(@RequestBody GetPracticeSubjectListReq req) {
        if (log.isInfoEnabled()) {
            log.info("获取练习题入参{}", JSON.toJSONString(req));
        }
        try {
            //参数校验
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(req.getAssembleIds()), "标签ids不能为空！");
            PracticeSubjectDTO dto = new PracticeSubjectDTO();
            dto.setAssembleIds(req.getAssembleIds());
            PracticeSetVO practiceSetVO = practiceSetService.addPractice(dto);
            if (log.isInfoEnabled()) {
                log.info("获取练习题目列表出参{}", JSON.toJSONString(practiceSetVO));
            }
            return Result.ok(practiceSetVO);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取练习题目列表异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取练习题目列表异常！");
        }
    }

    @RequestMapping("/getSubjects")
    public Result<PracticeSubjectListVO> getSubjects(@RequestBody GetPracticeSubjectsReq req) {
        if (log.isInfoEnabled()) {
            log.info("获取练习题入参{}", JSON.toJSONString(req));
        }
        try {
            //参数校验
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            Preconditions.checkArgument(!Objects.isNull(req.getSetId()), "套题id不能为空！");
            PracticeSubjectListVO practiceSubjectListVO = practiceSetService.getSubjects(req);
            if (log.isInfoEnabled()) {
                log.info("获取练习题目列表出参{}", JSON.toJSONString(practiceSubjectListVO));
            }
            return Result.ok(practiceSubjectListVO);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取练习题目列表异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取练习题目列表异常！");
        }
    }

    /**
     * 获取练习题目
     *
     * @return 练习题目列表
     */
    @RequestMapping("/getPracticeSubject")
    public Result<PracticeSubjectVO> getPracticeSubjects(@RequestBody GetPracticeSubjectReq getPracticeSubjectReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("获取练习题入参{}", JSON.toJSONString(getPracticeSubjectReq));
            }
            Preconditions.checkArgument(!Objects.isNull(getPracticeSubjectReq), "参数不能为空！");
            Preconditions.checkArgument(!Objects.isNull(getPracticeSubjectReq.getSubjectId()), "题目id不能为空！");
            Preconditions.checkArgument(!Objects.isNull(getPracticeSubjectReq.getSubjectType()), "题目类型不能为空！");
            PracticeSubjectDTO practiceSubjectDTO = new PracticeSubjectDTO();
            practiceSubjectDTO.setSubjectId(getPracticeSubjectReq.getSubjectId());
            practiceSubjectDTO.setSubjectType(getPracticeSubjectReq.getSubjectType());
            PracticeSubjectVO practiceSubjectVO = practiceSetService.getPracticeSubject(practiceSubjectDTO);
            if (log.isInfoEnabled()) {
                log.info("获取练习题目列表出参{}", JSON.toJSONString(practiceSubjectVO));
            }
            return Result.ok(practiceSubjectVO);
        } catch (Exception e) {
            log.error("获取练习题目列表异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取练习题目列表异常！");
        }

    }

    @RequestMapping("/getPreSetContent")
    public Result<PageResult<PracticeSetVO>> getPreSetContent(@RequestBody GetPreSetReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("获取练习题入参{}", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            PracticeSetDTO practiceSetDTO = new PracticeSetDTO();
            practiceSetDTO.setOrderType(req.getOrderType());
            practiceSetDTO.setPageInfo(req.getPageInfo());
            practiceSetDTO.setSetName(req.getSetName());
            PageResult<PracticeSetVO> preSetContent = practiceSetService.getPreSetContent(practiceSetDTO);
            if (log.isInfoEnabled()) {
                log.info("获取练习题目列表出参{}", JSON.toJSONString(preSetContent));
            }
            return Result.ok(preSetContent);
        } catch (Exception e) {
            log.error("获取练习题目列表异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取练习题目列表异常！");
        }
    }


}
